nums3 be the sorted array combining all the items in nums1 and nums2.nums2[j-1] <= nums1[i] <= nums2[j], then we know nums1[i] is at num3[i+j]. Same goes nums1[i-1] <= nums2[j] <= nums1[i].k be ⌊(m+n-1)/2⌋. We need to find nums3[k] (and also nums3[k+1] if m+n is even).i + j = k, if we find nums2[j-1] <= nums1[i] <= nums2[j] or nums1[i-1] <= nums2[j] <= nums1[i], then we got k.nums1[i] <= nums2[j] then we know nums1[i] < nums2[j-1] (because we did not find k).i items before nums1[i], and j-1 items brefor nums2[j-1], which means nums1[0...i] are before nums3[i+j-1]. So we now know nums1[0...i] < nums3[k]. They can be safely discarded.nums1[i] < nums2[j], which means nums2[j...n) are after nums3[i+j]. So nums2[j...n) > nums3[k].nums1[i-1] <= nums2[j] <= nums1[i]."PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)"PAHNAPLSIIGYIR"atoi which converts a string to an integer.' ' is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.Number() is faster than parseInt().s) and a pattern (p), implement regular expression matching with support for '.' and '*'.s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.l and r at the begin and end of a distance, and the area is area(l, r), how should we narrow down the distance?height[l] < height[r], we know that the height of the area will never be greater than height[l] if we keep l. Now if we get rid of r, the area can only get smaller since the distance is shorter, and the height is at most height[l].height[l] == height[r]? It is safe to get rid of both. We do not need any of them to constrain the max height of the rest points.I, V, X, L, C, D and M.II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:I can be placed before V (5) and X (10) to make 4 and 9.X can be placed before L (50) and C (100) to make 40 and 90.C can be placed before D (500) and M (1000) to make 400 and 900.I, V, X, L, C, D and M.II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:I can be placed before V (5) and X (10) to make 4 and 9.X can be placed before L (50) and C (100) to make 40 and 90.C can be placed before D (500) and M (1000) to make 400 and 900."".a-z.nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.sorted[0] > 0 or sorted[last] < 0, return an empty set.0 to len(sorted) - 2, pick sorted[i] as the first number of a possible triplet result.l = i + 1, r = len(sorted) - 1, we want to narrow them down to enumerate all possible combinations.l++ if sorted[i] + sorted[l] + sorted[r] > 0.r-- if sorted[i] + sorted[l] + sorted[r] < 0.nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.2-9 inclusive, return all possible letter combinations that the number could represent.Array.prototype.push accepts arbitrary arguments which enables tighter loops.nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.p1 for iterating, and p2 which is n nodes behind, pointing at the (n+1)-th node from the end of list.p2 could be one node before head, which means the head should be removed.p2 could be larger than the length of the list (Though the description says n will always be valid, we take care of it anyway).p1.next touches the end rather than p1 because we want p1 pointing at the last node.'(', ')', '{', '}', '[' and ']', determine if the input string is valid.1->2->3->4->52->1->4->3->53->2->1->4->5dividend and divisor, divide two integers without using multiplication, division and mod operator.dividend by divisor.a0*2^0 + a1*2^1 + a2*2^2 + ... + an*2^n.1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1[0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).-1.nums sorted in ascending order, find the starting and ending position of a given target value.[-1, -1].1-9 without repetition.1-9 without repetition.3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.'.'.1-9 and the character '.'.9x9.1-9 must occur exactly once in each row.1-9 must occur exactly once in each column.1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.'.'.1-9 and the character '.'.9x9.1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.candidates unlimited number of times.target) will be positive integers.candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.candidates may only be used once in the combination.target) will be positive integers.start is immune by the rule because we assume that the current group of candidates begins at start.[1,2,...,n]. The smallest missing positive integer is n+1.k into the slot indexed k-1 (via swapping).num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.num1 and num2 is < 110.num1 and num2 contain only digits 0-9.num1 and num2 do not contain any leading zero, except the number 0 itself.nums or use a set to mark.n = -2^31 might overflow.'Q' and '.' both indicate a queen and an empty space respectively.n-length array queens. Each item represents a queen coordinate on the borad. Let index i be the row index, and queens[i] be the column index (or vice versa).nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.f(i) to be the largest sum of a contiguous subarray that ends with nums[i].f(i-1) is negative, then nums[i] must be greater than f(i-1) + nums[i].nums[n-2] covers nums[n-1], then we can safely make n-2 the new destination point, and so on.nums[0] can cover the last destination point, it is good.' ', return the length of last word in the string.split will guarantee that there is at least one item in the resulted array.[1,2,3,...,*n*] contains a total of n! unique permutations."123""132""213""231""312""321"[1...n].(n-1)! times in result[0]. And for a fixed result[0] each digit appears (n-2)! times in result[1]. So on.k-- to convert k into index so that k <= (n-1)! maps 0 (and get 1 from the set).k could be larger than the length of this list.k is larger than the length.k % len.f(i, j) to be the number of total unique paths from (0, 0) to (i, j).f(i, j) to be the min sum from (0, 0) to (i, j)."0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => trueC++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.Math.abs will first convert the argument to number.Math.abs(' ') === 0.isNaN will first convert the argument to number.isNaN(' ') === false.unshift is much slower that expanding.' ' when necessary so that each line has exactly maxWidth characters.words contains at least one word.Math.ceil.int sqrt(int x)."/home/", => "/home"
path = "/a/./b/../../c/", => "/c""/../"?
In this case, you should return "/".'/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo"./../.f(i, j) to be the min edit distance from word1[0...i) to word2[0...j).NaN to mark cells that need to be set 0.NaNs.n slices.1 as pivot.0s and 2s so that we know where to swap.NaN before proceeding to the next level and restore when backtracking.[0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).true, otherwise return false.pivot == nums[e]. The only thing we can do is to ditch nums[e]. SO worst case O(*n*).p1 points to the current node. p points to the node before p1 so that we can ditch p1 if needed.dupVal to keep the latest duplicate value.[2,1,5,6,2,3].10 unit.len(heights) rectangles. Return the max area.b whose height is h, find the closest bar b1 on the left that is lower than h, and b2 on the right. The area of the rectangle is h * (i2 - i1 - 1).b1 and b2 of each bar may overlap.iheights[i]i2 - i1 - 1height * widthi1 and i2 changes depending on the height.bs. Because b1 and b2 are both lower than b, whenever we reach a bar that is lower than the top of the stack, we know it's a b2. So stack top is a b. Second top is a b1. Keep popping the b to calculate areas until b2 is no longer lower than stack top.height(matrix) times the problem of 84. Largest Rectangle in Histogram.(row, col) and assume it is on the base line. The adjoining 1s above (row, col) makes up the height of the rectangle. The width of the rectangle is determined by how many ajoining bars are taller than the pivot bar.(row, col):area(row, col) to be the area.height(row, col) to be the height.left(row, col) to be the col value of the bottom-left corner.right(row, col) to be the col value of the bottom-right corner.conLeft(row, col) to be the col value of the leftmost cell of the consecutive 1s on the left of (row, col).conRight(row, col) to be the col value of the rightmost cell of the consecutive 1s on the right of (row, col).conLeft and conRight we can know if the rectangle on (row, col) shrinks comparing to (row-1, col).nums1 is guaranteed longer than nums2 so just use n as boundary.A-Z is being encoded to numbers using the following mapping:f(i) to be the number of ways to decode s[0...i].'0'.f(-1) = 1 for easy calculation.'0' is not allowed except just '0'.f(i, j) to be whether s3[0...i+j-1) can be formed by the interleaving of s1[0...i) and s2[0...j).[1,2,2,3,4,4,3] is symmetric:[1,2,2,null,3,null,3] is not:[3,9,20,null,null,15,7],[3,9,20,null,null,15,7],[3,9,20,null,null,15,7],pre(root) = root + pre(root.left) + pre(root.right)in(root) = in(root.left) + root + in(root.right)in(root) = in(root.left) + root + in(root.right)post(root) = post(root.left) + post(root.right) + root[3,9,20,null,null,15,7],[3,9,20,null,null,15,7]:[1,2,2,3,3,null,null,4,4]:-1.[3,9,20,null,null,15,7],null children.sum = 22,5->4->11->2 which sum is 22.sum = 22,"ACE" is a subsequence of "ABCDE" while "AEC" is not).f(i, j) to be the number of ways that generate T[0...j) from S[0...i).f(i, j) you can always skip S[i-1], but can only take it when S[i-1] === T[j-1].NULL.NULL.node:node.right.node.next.left if node.next exists.NULL.NULL.next until there is a node with children, or null.(i, j) depends on (i-1, j) and (i-1, j-1). So to access (i-1, j-1) iteration must be from right to left.11 (i.e., 2 + 3 + 5 + 1 = 11).f(i, j) to be the minimum path sum from triangle[0][0] to triangle[i][j].j whose price price[j] is lower than the base (which means if we sell now the profit would be negative), we sell j-1 instead and make j the new base.price[j] is lower that the base, using j as new base is guaranteed to gain more profit comparing to the old one.[0...k] and [k...n-1]. Notice k exists in both groups because technically we can sell out then immediately buy in at the same day.p1(i) to be the max profit of day [0...i]. This is just like the problem of 121. Best Time to Buy and Sell Stock.p2(i) to be the max profit of day [i...n-1]. This is the mirror of p1.f(k) to be p1(k) + p2(k). We need to get max( f(0), ..., f(n-1) ).node, there are six possible ways to get the max path sum:node.valnode.val plus the max sum of a path that ends with node.left.node.val plus the max sum of a path that starts with node.right.node.val plus the max sum of both paths.node.val (the max sum of both paths are negative).node.val (disconnected)node.left subtree.node.right subtree.root node, and the max sum of the path that ends with root node.root). Maintain a global variable to store the disconnected max sum.begineWord as root and endWord as leaves.i) we encounter a word that turns out to be in a shortest path, then level i is the lowest level this word could ever get. We can safely remove it from the wordList.wordList, enumerate all 25 possible words and check if in wordList.0-9 only, each root-to-leaf path could represent a number.1->2->3 which represents the number 123.0 as indicator of leaf node. If all the children get 0, it is a leaf node, return the sum of current level.'X' and 'O' (the letter O), capture all regions surrounded by 'X'.'O's into 'X's in that surrounded region.'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.Os that are connected to the Os on the border, change them to #. Then scan the board, change O to X and # back to O.Os is just like tree traversal. Os on the border are the same level. Their children are the second level. And so on.label (int) and a list (List[UndirectedGraphNode]) of its neighbors. There is an edge between the given node and each of the nodes in its neighbors.# as a separator for each node, and , as a separator for node label and each neighbor of the node.{0,1,2#1,2#2,2}.#.0. Connect node 0 to both nodes 1 and 2.1. Connect node 1 to node 2.2. Connect node 2 to node 2 (itself), thus forming a self-cycle.Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
[0, 5000].-104 <= Node.val <= 104Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
O(height of tree)?[0, 104].-105 <= Node.val <= 105root is a valid binary search tree.-105 <= key <= 105